package su.samlib.informer;

import su.samlib.informer.database.controllers.LibraryJpaController;
import su.samlib.informer.database.model.Author;
import su.samlib.informer.database.model.Book;
import su.samlib.informer.database.model.Library;
import su.samlib.informer.utils.updaters.BooksUpdater;
import su.samlib.informer.utils.updaters.config.Config;
import su.samlib.informer.utils.updaters.config.ConfigFactory;
import su.samlib.informer.utils.updaters.samizdatupdaters.SamizdatPlainTextBooksUpdater;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.io.IOException;
import java.util.*;

class UpdateTask extends TimerTask {

    @Override
    public void run() {
        Library library = new Library();
        library.setLibraryUrl("http://samlib.ru");
        library.setLibraryTitle("Самиздат");
        Author a = new Author();
        a.setAuthorUri("/b/burewojandrej/");
        a.setAuthorFio("Буревой");
        a.setLibraryId(library);
        List<Author> al = new ArrayList<Author>();
        al.add(a);
        library.setAuthorList(al);
        BooksUpdater tu = new SamizdatPlainTextBooksUpdater();
        tu.setAuthor(a);
        List<Book> books = tu.getBooks();
        for (Book b : books) {
            System.out.print(b.getBookTitle());
            System.out.print(": ");
            System.out.println(b.getBookUri());
        }
    }
}

public class Main {

    private static EntityManagerFactory factor;
    private static Config dbConfig = ConfigFactory.newInstance().getConfigDatabase();
    private static Config baseConfig = ConfigFactory.newInstance().getConfigBase();


    private static Map<String,String> additionalProperties = new HashMap<String, String>();
    static {
        String jdbc_url = dbConfig.getProperty("jdbc_url");
        if(jdbc_url != null) {
            additionalProperties.put("javax.persistence.jdbc.url",jdbc_url);
        }
        String jdbc_user = dbConfig.getProperty("jdbc_user");
        if(jdbc_user != null) {
            additionalProperties.put("javax.persistence.jdbc.user",jdbc_user);
        }
        String jdbc_password = dbConfig.getProperty("jdbc_password");
        if(jdbc_password != null) {
            additionalProperties.put("javax.persistence.jdbc.password",jdbc_password);
        }
        factor = Persistence.createEntityManagerFactory("sinta-server-projectPU",additionalProperties);

    }


    public static void main(String... args) throws IOException {
//        Timer t = new Timer();
//        TimerTask tt = new UpdateTask();
//        t.schedule(tt, 0, 200);
//        tt.run();
//        LibraryJpaController jpaLibraryController = new LibraryJpaController(factor);
//        List<Library> libraries = jpaLibraryController.findLibraryEntities();
//        for(Library l: libraries) {
//            System.out.println("--------------------");
//            System.out.println(l.getLibraryTitle());
//            System.out.println(l.getLibraryUrl());
//        }
//        try {
//             Library l = libraries.get(0);
//            l.setLibraryTitle("Самиздат");
//            jpaLibraryController.edit(l);
//        } catch (PreexistingEntityException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        LibraryJpaController jpaLibraryController = new LibraryJpaController(factor);
        Library l = new Library();
        l.setLibraryTitle("Самиздат");
        l.setLibraryUrl("http://samlib.ru");
        jpaLibraryController.create(l);
//        List<Library> libraries = jpaLibraryController.findLibraryEntities();

        System.out.println("Done.");
    }
}
